package IC.Types;

import java.util.*;

public class MethodType extends Type {

	public MethodType(Type returnType, List<Type> paramTypes){
		super(null);
		this.returnType = returnType;
		this.paramTypes = paramTypes;
	}
	
	public Type getReturnType(){
		return this.returnType;
	}
	
	public List<Type> getParamTypes(){
		return this.paramTypes;
	}

	public boolean equals(MethodType mt){
		if (this.getName() != mt.getName()) {
			return false;
		}
		else if (this.returnType != mt.getReturnType()){
			return false;
		}
		else{
			Iterator<Type> myIter = this.paramTypes.iterator();
			Iterator<Type> otherIter = mt.paramTypes.iterator();
			
			while (myIter.hasNext() && otherIter.hasNext()){
				if (myIter.next() != otherIter.next()) {
					return false; 
				}
			}
			if (myIter.hasNext() || otherIter.hasNext()){
				return false;
			}
			else {
				return true;
			}
		}
	}
	
	public String toString(){
		String str = "{";
		Iterator<Type> paramIter = paramTypes.iterator();
		if (paramIter.hasNext()) {
			str += paramIter.next().getName();
		}
		while (paramIter.hasNext()) {
			str += ", "+paramIter.next().getName();
		}		
		str += " -> "+this.returnType.getName()+"}";	
		return str;
	}
	
	public boolean subtypeOf(Type t){
		if (t == this) {
			return true;
		}
		else {
			return false;
		}
	}
	
	private List<Type> paramTypes;
	private Type returnType;
	
}